"""配置文件读取"""

import os, json5
from functools import wraps


def __path_beautiful(func):
    # TODO 增加windows的路径适配
    """路径优化，当路径中最后一个字符不存在/时，自动添加"""

    @wraps(func)
    def wapper(*args, **kwargs):
        result = func(*args, **kwargs)
        if "path" in args[0]:
            new_result = ""
            if result is not None:
                for i in result.split("/"):
                    if i not in ["/"]:
                        new_result = new_result + i + "/"
            return new_result
        return result

    return wapper


@__path_beautiful
def get_conf(path):
    """
    读取配置文件
    若读取的是路径，则路径后面会自动添加/
    """
    with open(os.getcwd() + "/conf/config.json5", "r+", encoding="utf-8") as json_conf:
        current_data = json5.load(json_conf)
        for key in path.split("."):
            # 如果 key 是数字，表示是列表的索引
            if key.isdigit():
                key = int(key)
                if isinstance(current_data, list) and key < len(current_data):
                    current_data = current_data[key]
                else:
                    return None
            elif key in current_data:
                current_data = current_data[key]
            else:
                return None
        return str(current_data)
